package com.rowline.designpatterndemo.math;

import java.util.Stack;

public class CountAndSaySolution {


    public static String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }

        Stack<Integer> stack = new Stack<>();


        String t = countAndSay(n - 1);

        int j = 0;
        char tmp = t.charAt(0);
        String s = "";
        for (int i = 0; i < t.length(); i++) {
            if (tmp == t.charAt(i)) {
                j++;
            } else {
                s = s + j + tmp;
                tmp = t.charAt(i);
                j = 1;
            }
        }
        s = s + j + tmp;


        return s;


    }


    public static void main(String[] args) {
        System.out.println(countAndSay(7));
    }
}
